﻿
'if(value) paragraph else paragraph
Public Class condition
    Inherits sentence
    Public Const [if] As String = "if"
    Public Const [else] As String = "else"

    Protected Friend Shared Shadows Function _isMeaningfulMark(ByVal c As Char) As Boolean
        Return [false](c)
    End Function

    Private condition As value = Nothing
    Private truepara As paragraph = Nothing
    Private falsepara As paragraph = Nothing

    Private Sub New(ByVal icond As value, ByVal itrue As paragraph, ByVal ifalse As paragraph)
        condition = icond
        truepara = itrue
        falsepara = ifalse
    End Sub

    Protected Overrides Function invokeImpl(ByVal ctx As context) As Boolean
        Dim v As bare_variable = Nothing
        If condition.invoke(ctx, v) Then
            If v.true() Then
                Return truepara.invoke(ctx)
            ElseIf Not falsepara Is Nothing Then
                Return falsepara.invoke(ctx)
            Else
                Return True
            End If
        Else
            Return False
        End If
    End Function

    Private Shared Function ifstart(ByVal input As String, ByVal index As Int64, ByRef inc As Int64) As Boolean
        If strsame(input, index, [if], 0, strlen([if])) Then
            inc = strlen([if])
            Return True
        Else
            Return False
        End If
    End Function

    Private Shared Function elsestart(ByVal input As String, ByVal index As Int64, ByRef inc As Int64) As Boolean
        If strsame(input, index, [else], 0, strlen([else])) Then
            inc = strlen([else])
            Return True
        Else
            Return False
        End If
    End Function

    Protected Shared Function parseImpl(ByVal input As String, ByRef index As Int64, _
                                        ByRef output As condition) As Boolean
        If token(input, index, Nothing, AddressOf ifstart, AddressOf [false]) Then
            Dim value As value = Nothing
            If value.parse(input, index, value) Then
                Dim truepara As paragraph = Nothing
                If paragraph.parse(input, index, truepara) Then
                    If token(input, index, Nothing, AddressOf elsestart, AddressOf [false]) Then
                        Dim falsepara As paragraph = Nothing
                        If paragraph.parse(input, index, falsepara) Then
                            output = New condition(value, truepara, falsepara)
                            Return True
                        End If
                    Else
                        output = New condition(value, truepara, Nothing)
                        Return True
                    End If
                End If
            End If
        End If

        Return False
    End Function

    Protected Overrides Sub setsource(ByVal script As String, ByVal start As Long, ByVal [end] As Long)
        setsourceImpl([if] + character.blank + condition.scriptSource(), start, [end])
    End Sub

    Public Shared Shadows Function parse(ByVal input As String, ByRef index As Int64, _
                                         ByRef output As condition) As Boolean
        Return parsebase(input, index, output, AddressOf parseImpl)
    End Function
End Class
